/**
 * 文章数据函数
 */
//Article类继承封装好的数据库查询类
module.exports = class Article extends require('./model1') {
    /**
     *
     */

    static getDetails(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT u_id,title,content,contentText,`time`,thumbnail ,hits , hot FROM `article` WHERE id = ?'
            this.query(sql, id).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取文章失败:${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 搜索获取并且分页
     * @param text
     * @param index
     * @param yenum
     * @returns {Promise<any>}
     */
    static searchCent(text, index, yenum,) {
        return new Promise((resolve, reject) => {
            let sql = "SELECT id,title,content,contentText,`time`,thumbnail ,hits FROM article WHERE contentText LIKE '%" + text + "%' LIMIT " + index + "," + yenum + "";
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取文章失败:${err.message}`)
                reject(err)
            })
        })

    }


    /**
     * 获取所有并且分页
     * @param text
     * @param index
     * @param yenum
     * @returns {Promise<any>}
     */
    static getAll(index, yenum,) {
        return new Promise((resolve, reject) => {
            let sql = "SELECT id,title,content,contentText,`time`,thumbnail ,hits FROM article LIMIT " + index + "," + yenum + "";
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取文章失败:${err.message}`)
                reject(err)
            })
        })

    }


    /**
     * 热门文章且分页,返回给中间件
     * @param num 分页数
     * @returns {Promise<any>}
     */
    static getHots(num) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,content,contentText,`time`,thumbnail ,hits FROM article WHERE hot = 1 LIMIT ?';
            this.query(sql, num).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取热门推荐文章失败:${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 发布文章的内容与html写入模型
     * @param text
     * @param htmltext
     * @param u_id
     * @param imgSrcS
     * @param title
     * @constructor
     */
    static Releasemod(text, htmltext, u_id, imgSrcS, title) {

        return new Promise((resolve, reject) => {
            let sql = "insert into article (content,contentText,title,u_id,thumbnail) values(?,?,?,?,?)"
            this.query(sql, this.formatParams(htmltext, text, title, u_id, imgSrcS)).then(result => {
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })
    }


    /**
     * 我的历史博客且分页,返回给中间件
     * @param num 分页数
     * @returns {Promise<any>}
     */
    static myHistory(u_id, num, yenum) {

        num = Number(num)
        yenum = Number(yenum)
        return new Promise((resolve, reject) => {
            let sql = 'SELECT  id,hot,title,content,contentText,`time`,thumbnail ,hits FROM article WHERE u_id= ' + u_id + ' LIMIT ?,?'
            this.query(sql, this.formatParams(num, yenum)).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取热门推荐文章失败:${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 根据文章id插入评论
     * @param u_id
     * @param article_id
     * @param content
     * @returns {Promise<any>}
     */
    static setComment(u_id, article_id, content) {
        article_id = Number(article_id)
        u_id = Number(u_id)
        return new Promise((resolve, reject) => {
            let sql = "insert into comment (u_id,article_id,content) values(?,?,?) "
            this.query(sql, this.formatParams(u_id, article_id, content)).then(result => {
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })
    }


    static getComment(article_id, index, yenum) {
        article_id = Number(article_id)
        return new Promise((resolve, reject) => {
            // let sql1=`CONVERT_TZ(xxx,‘+00:00’,‘+08:00’)`
            // let sqls='SELECT * FROM `comment` order by id desc WHERE article_id = ? LIMIT ?,?'
            let sql = 'SELECT * FROM `comment` WHERE article_id = ' + article_id + ' order by id desc '
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`获取评论失败:${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 添加文章点击量
     * @param article_id
     * @returns {Promise<any>}
     */
    static addHist(article_id) {
        article_id = Number(article_id)
        return new Promise((resolve, reject) => {
            let sql = 'update article set hits=hits+1 where id = ?'
            this.query(sql, article_id).then(result => {
                resolve(result)
            }).catch(err => {
                console.log(`添加点击量失败:${err.message}`)
                reject(err)
            })
        })

    }

}